home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / c / stdlib / RCS / setenv.c,v < prev    next >
Encoding:
Text File  |  1991-12-05  |  3.4 KB  |  155 lines

  1. head     1.2;
  2. branch   ;
  3. access   ;
  4. symbols  sprited:1.2.1;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.2
  10. date     88.07.25.11.11.00;  author ouster;  state Exp;
  11. branches 1.2.1.1;
  12. next     1.1;
  13.  
  14. 1.1
  15. date     88.05.21.16.17.54;  author ouster;  state Exp;
  16. branches ;
  17. next     ;
  18.  
  19. 1.2.1.1
  20. date     91.12.04.16.32.53;  author kupfer;  state Exp;
  21. branches ;
  22. next     ;
  23.  
  24.  
  25. desc
  26. @@
  27.  
  28.  
  29. 1.2
  30. log
  31. @Lint.
  32. @
  33. text
  34. @/* 
  35.  * setenv.c --
  36.  *
  37.  *    Contains the source code for the "setenv" library procedure.
  38.  *
  39.  * Copyright 1988 Regents of the University of California
  40.  * Permission to use, copy, modify, and distribute this
  41.  * software and its documentation for any purpose and without
  42.  * fee is hereby granted, provided that the above copyright
  43.  * notice appear in all copies.  The University of California
  44.  * makes no representations about the suitability of this
  45.  * software for any purpose.  It is provided "as is" without
  46.  * express or implied warranty.
  47.  */
  48.  
  49. #ifndef lint
  50. static char rcsid[] = "$Header: setenv.c,v 1.1 88/05/21 16:17:54 ouster Exp $ SPRITE (Berkeley)";
  51. #endif not lint
  52.  
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55.  
  56. extern char **environ;
  57.  
  58. /*
  59.  *----------------------------------------------------------------------
  60.  *
  61.  * setenv --
  62.  *
  63.  *    Associate the value "value" with the environment variable
  64.  *    "name" in this process's environment.
  65.  *
  66.  * Results:
  67.  *    None.
  68.  *
  69.  * Side effects:
  70.  *    The storage for the environment is modified.  If there already
  71.  *    was an environment variable by the given name, then it is
  72.  *    replaced.  Otherwise a new environment variable is created.
  73.  *    The new value will be visible to this process, and also will
  74.  *    be passed to children processes.
  75.  *
  76.  *----------------------------------------------------------------------
  77.  */
  78.  
  79. void
  80. setenv(name, value)
  81.     char *name;            /* Name of environment variable. */
  82.     char *value;        /* (New) value for variable. */
  83. {
  84.     register int    i;
  85.     register char **envPtr;
  86.     register char **newEnvPtr;
  87.     register char *charPtr;
  88.     register char *namePtr;
  89.     char *newEnvValue;
  90.  
  91.     newEnvValue = malloc ((unsigned) (strlen (name) + strlen (value) + 2));
  92.     if (newEnvValue == 0) {
  93.     return;
  94.     }
  95.     (void) sprintf(newEnvValue, "%s=%s", name, value);
  96.  
  97.     /*
  98.      * Although this procedure allocates new storage when necessary,
  99.      * it can't de-allocate the old storage, because it doesn't know
  100.      * which things were allocated with malloc and which things were
  101.      * allocated statically when the process was created.
  102.      */
  103.  
  104.     for (envPtr = environ, i=0; *envPtr != 0; envPtr++, i++) {
  105.     for (charPtr = *envPtr, namePtr = name;
  106.          *charPtr == *namePtr; namePtr++) {
  107.          charPtr++;
  108.          if (*charPtr == '=') {
  109.          namePtr++;
  110.          if (*namePtr == '\0') {
  111.              *envPtr = newEnvValue;
  112.              return;
  113.          }
  114.          break;
  115.          }
  116.      }
  117.     }
  118.     newEnvPtr = (char **) malloc ((unsigned) ((i + 2) * sizeof *newEnvPtr));
  119.     if (newEnvPtr == 0) {
  120.     return;
  121.     }
  122.     for (envPtr = environ, i = 0; *envPtr; envPtr++, i++) {
  123.     newEnvPtr[i] = *envPtr;
  124.     }
  125.     newEnvPtr[i] = newEnvValue;
  126.     newEnvPtr[i+1] = 0;
  127.     environ = newEnvPtr;
  128. }
  129. @
  130.  
  131.  
  132. 1.2.1.1
  133. log
  134. @Initial branch for Sprite server.
  135. @
  136. text
  137. @d17 1
  138. a17 1
  139. static char rcsid[] = "$Header: /sprite/src/lib/c/stdlib/RCS/setenv.c,v 1.2 88/07/25 11:11:00 ouster Exp $ SPRITE (Berkeley)";
  140. @
  141.  
  142.  
  143. 1.1
  144. log
  145. @Initial revision
  146. @
  147. text
  148. @d17 1
  149. a17 1
  150. static char rcsid[] = "$Header: proto.c,v 1.2 88/03/11 08:39:08 ouster Exp $ SPRITE (Berkeley)";
  151. d20 2
  152. a21 1
  153. #include "stdlib.h"
  154. @
  155.